<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                </div>
            </script>
            <table class="layui-hide" lay-size="lg" id="currentTableId" lay-filter="currentTableFilter"></table>
            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            </script>
        </div>
    </div>
    <script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="../js/lay-config.js"></script>
    <script>
        layui.use(['form', 'table', 'common'], function () {
            var $ = layui.jquery,
                form = layui.form,
                common = layui.common,
                table = layui.table;

            function initTable() {
                common.ajax("/QLPanel", "get", {}, function (data) {

                    table.render({
                        elem: '#currentTableId',
                        //url: '../api/table.json',
                        toolbar: '#toolbarDemo',
                        defaultToolbar: ['filter', 'exports'],
                        cols: [[
                            { type: "checkbox", width: 50 },
                            { field: 'Name', title: '面板名称', sort: true },
                            { field: 'Address', title: '面板地址', sort: true },
                            //{ field: 'UserName', title: '用户名' },
                            //{
                            //    field: 'PassWord', title: '密码', sort: true, templet(d) {
                            //        return "******";
                            //    }
                            //},
                            { field: 'ClientID', title: 'ClientID' },
                            {
                                field: 'ClientSecret', title: 'ClientSecret', sort: true, templet(d) {
                                    return "******";
                                }
                            },
                            { field: 'MaxCount', width: 90, title: '数量上限' },
                            { field: 'CookieCount', width: 90, title: 'CK数量' },
                            { field: 'Weigth', title: '权重' },
                            {
                                field: 'EnableAll', title: '全量', sort: true, templet(d) {
                                    return d.EnableAll ? "是" : "否";
                                },
                                width: 80
                            },
                            { title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center" }
                        ]],
                        limits: [10, 15, 20, 25, 50, 100],
                        limit: 15,
                        page: true,
                        skin: 'line',
                        data: data,
                        size: "lg"
                    });
                })
            }
            initTable();
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加青龙面板',
                        type: 2,
                        shade: 0.2,
                        area: ['800px', '700px'],
                        content: '../page/qinglong-add.html',
                        end: initTable
                    });
                } else if (obj.event === 'delete') {  // 监听删除操作
                    var checkStatus = table.checkStatus('currentTableId')
                        , data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {
                    var index = layer.open({
                        title: '编辑青龙面板',
                        type: 2,
                        shade: 0.2,
                        area: ['800px', '700px'],
                        content: '../page/qinglong-add.html?id=' + data.Id,
                        end: initTable
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('真的要删除吗？删除后QQBot中对应的Cookie将同步删除，且无法恢复！', function (index) {
                        obj.del();
                        common.ajax("/QLPanel/" + data.Id, "delete", {}, function () {
                            layer.msg("删除成功。", { icon: 6 });
                            layer.close(index);
                        })
                    });
                } else if (obj.event === 'sync') {
                    layer.confirm('确定重新同步Cookie信息吗？', function (index) {
                        common.ajax(`/JDCookie/Sync`, "get", {}, function (data) {
                            layer.msg("同步操作成功，请稍后刷新账号查看！", { icon: 6 });
                        });
                    });
                }
            });
        });
    </script>

</body>
</html>